package com.jdd.modules.parking.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdd.common.api.vo.Result;
import com.jdd.common.aspect.annotation.*;
import com.jdd.common.system.query.QueryGenerator;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.ChargeUtil3;
import com.jdd.common.util.DateUtil;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.modules.configcartype.entity.ConfigCarType;
import com.jdd.modules.configcartype.service.IConfigCarTypeService;
import com.jdd.modules.parking.entity.ConfigParkCharge;
import com.jdd.modules.parking.service.IConfigParkChargeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 停车场计费规则信息表
 * @Author: jdd
 * @Date: 2021-02-18
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "停车场计费规则信息表")
@RestController
@RequestMapping("/parking/configParkCharge")
public class ConfigParkChargeController {

    @Resource
    private IConfigParkChargeService configParkChargeService;
    @Resource
    private IConfigCarTypeService configCarTypeService;

    /**
     * 分页列表查询
     *
     * @param configParkCharge
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "停车场计费规则信息表-分页列表查询并翻译")
    @ApiOperation(value = "停车场计费规则信息表-分页列表查询并翻译", notes = "停车场计费规则信息表-分页列表查询并翻译")
    @GetMapping(value = "/Translist")
    public Result<IPage<ConfigParkCharge>> queryPageListAndTrans(ConfigParkCharge configParkCharge,
                                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                 String startTime, String endTime,
                                                                 HttpServletRequest req) {
        Result<IPage<ConfigParkCharge>> result = new Result<>();
        QueryWrapper<ConfigParkCharge> queryWrapper = QueryGenerator.initQueryWrapper(configParkCharge, req.getParameterMap());
        Page<ConfigParkCharge> page = new Page<>(pageNo, pageSize);
        IPage<ConfigParkCharge> pageList = configParkChargeService.page(page, queryWrapper);
        for (int i = 0; i < pageList.getRecords().size(); i++) {
            ConfigParkCharge item = pageList.getRecords().get(i);
            ConfigCarType car = configCarTypeService.getById(item.getCarTypeId());
            if (car != null && car.getCarTypeName() != null) {
                pageList.getRecords().get(i).setCarTypeId(car.getCarTypeName());
            } else {
                pageList.getRecords().get(i).setCarTypeId("请重新配置");
            }
        }
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     *
     * @param configParkCharge
     * @return
     */
    @AutoLog(value = "停车场计费规则信息表-添加")
    @ApiOperation(value = "停车场计费规则信息表-添加", notes = "停车场计费规则信息表-添加")
    @PostMapping(value = "/add")
    @OperationLogDetail(detail ="停车场计费规则，添加",level= LogLevelType.ONE,operationType = OperationType.INSERT,operationUnit = OperationUnit.UNKNOWN)
    public Result<ConfigParkCharge> add(@RequestBody ConfigParkCharge configParkCharge) {
        Result<ConfigParkCharge> result = new Result<>();
        try {
            LoginUser sysUser =
                    (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String name = sysUser.getUsername();
            configParkCharge.setId(SnowFlakeUtil.getId());
            configParkCharge.setCreateUser(name);
            configParkChargeService.saveEntity(configParkCharge);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param configParkCharge
     * @return
     */
    @AutoLog(value = "停车场计费规则信息表-编辑")
    @ApiOperation(value = "停车场计费规则信息表-编辑", notes = "停车场计费规则信息表-编辑")
    @OperationLogDetail(detail ="停车场计费规则，编辑",level= LogLevelType.ONE,operationType = OperationType.UPDATE,operationUnit = OperationUnit.UNKNOWN)
    @PutMapping(value = "/edit")
    public Result<ConfigParkCharge> edit(@RequestBody ConfigParkCharge configParkCharge) {
        Result<ConfigParkCharge> result = new Result<>();
        ConfigParkCharge configParkChargeEntity = configParkChargeService.findConfigParkChargeById(configParkCharge.getId());
        if (configParkChargeEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = configParkChargeService.updateEntity(configParkCharge);
            //TODO 返回false说明什么？
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "停车场计费规则信息表-通过id删除")
    @ApiOperation(value = "停车场计费规则信息表-通过id删除", notes = "停车场计费规则信息表-通过id删除")
    @DeleteMapping(value = "/delete")
    @OperationLogDetail(detail ="停车场计费规则，通过id删除",level= LogLevelType.ONE,operationType = OperationType.DELETE,operationUnit = OperationUnit.UNKNOWN)
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            ConfigParkCharge configParkCharge = configParkChargeService.findConfigParkChargeById(id);
            if(configParkCharge == null)
            {
                return Result.error("该数据不存在");
            }
            configParkChargeService.deleteEntityById(configParkCharge.getId());
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    @AutoLog(value = "停车场计费规则信息表-获取指定车场指定车辆的计费信息")
    @ApiOperation(value = "停车场计费规则信息表-获取指定车场指定车辆的计费信息", notes = "停车场计费规则信息表-获取指定车场指定车辆的计费信息")
    @RequestMapping(value = "/GetParkCharge")
    public Result<List<ConfigParkCharge>> editCharge(@RequestBody ConfigParkCharge configParkCharge) {
        Result<List<ConfigParkCharge>> result = new Result<>();
        List<ConfigParkCharge> configParkChargeList = new ArrayList<>();
        String carTypeId = configParkCharge.getCarTypeId();
        //根据传入的车辆类型ID定义查询条件
        ConfigParkCharge parkCharge = configParkChargeService.findConfigParkChargeByCarTypeId(carTypeId);
        if(parkCharge != null)
        {
            configParkChargeList.add(parkCharge);
        }
        result.setResult(configParkChargeList);
        result.setSuccess(true);
        return result;
    }

    @AutoLog(value = "停车场计费规则配置-测试停车场计费规则计费金额")
    @ApiOperation(value = "停车场计费规则配置-测试停车场计费规则计费金额", notes = "停车场计费规则配置-通过计费规则id,carEnterTime,carOutTime查询")
    @GetMapping(value = "/testChargeFee")
    public Result<?> chargeFeeTest(@RequestParam("id") String id, @RequestParam("carEnterTime") String carEnterTime,
                                   @RequestParam("carOutTime") String carOutTime) {

        ConfigParkCharge one = configParkChargeService.getById(id);
        if (null == one) {
            return Result.error("获取计费金额失败");
        }
        Date enterTime = DateUtil.fomatDateTime(carEnterTime);
        Date outTime = DateUtil.fomatDateTime(carOutTime);
        int chargeFee = (int) ChargeUtil3.totalFeeCal(one, enterTime, outTime);

        return Result.ok(chargeFee);
    }
}
